package com.zllkj.mm.module.home.fragments

import android.os.Bundle
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zllkj.mm.R
import com.zllkj.mm.adapter.CollectionListAdapter
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.model.entity.CollectionsListBean
import com.zllkj.mm.module.goods.GoodsDetailFragment
import com.zllkj.mm.module.goods.vm.ItemCollectionsVM
import com.zllkj.mm.module.shopping.fragment.OrderListItemFragment
import com.zllkj.mm.util.common.FormatUtil
import kotlinx.android.synthetic.main.fragment_mycollection.view.*
import kotlinx.android.synthetic.main.fragment_order_list.*
import kotlinx.android.synthetic.main.fragment_order_list.view.*
import java.util.HashMap

/**
 * 我的收藏
 */
class MyCollectionFragment : BaseTitleFragment() {
    var datas = ArrayList<ItemCollectionsVM>()
    lateinit var adapter : CollectionListAdapter
    var currentPage = 1
    override fun getContentLayoutId(): Int {
        return R.layout.fragment_mycollection
    }

    override fun loadData() {
        super.loadData()
        setCommonTitle("收藏")
        adapter = CollectionListAdapter(R.layout.item_collection_goods_list,datas)
        var emptyView = layoutInflater.inflate(R.layout.include_empty_layout,null).apply {
            findViewById<AppCompatImageView>(R.id.iv_empty_icon).setImageResource(R.mipmap.ic_empty_collection)
            findViewById<AppCompatTextView>(R.id.tv_empty_title).text = "这里空空如也"
            findViewById<AppCompatTextView>(R.id.tv_empty_content).text = "“藏藏更健康~”"
        }
        adapter.emptyView = emptyView
        mRootView.rv_mycollection.layoutManager = LinearLayoutManager(requireContext())
        mRootView.rv_mycollection.adapter = adapter


        adapter.setOnItemClickListener { baseQuickAdapter, view, i ->
            addFragment(GoodsDetailFragment(datas[i].id))
        }


        mRootView?.smartRefreshlayout_mycollection?.setOnRefreshListener {
            datas.clear()
            currentPage = 1
            loadNetWorkData(OrderListItemFragment.PULL_DOWN)
        }

        mRootView?.smartRefreshlayout_mycollection?.setOnLoadMoreListener {
            currentPage += 1
            loadNetWorkData(OrderListItemFragment.LOAD_MORE)
        }
        loadNetWorkData(-1)
    }

    fun loadNetWorkData(loadStatus:Int){
        addSubscribe(RetrofitHelper.getInstance().goodsApi.getGoodsCollectionsList((HashMap<String, Any>().apply {
            this["goodsName"] = ""
            this["currentPage"] = currentPage
            this["pageSize"] = 20
        })).compose<CollectionsListBean>(RxHelper.defaultHandleResult<CollectionsListBean>()).subscribe(object : ProgressSubscriber<CollectionsListBean>(requireContext()){
            override fun onSucc(t: CollectionsListBean?) {

                t?.records?.map {
                    var vm = ItemCollectionsVM()
                    vm.name = it.goodsName
                    vm.des = ""
                    vm.id = it.goodsId.toString()
                    vm.pic = it.goodsPic
                    vm.unitPrice = FormatUtil.bigDecimalUtilFormat(it.unitPrice.toString(),"100")
                    datas.add(vm)
                }
                if(loadStatus == OrderListItemFragment.PULL_DOWN) {
                   mRootView.smartRefreshlayout_mycollection.finishRefresh()
                    adapter.notifyDataSetChanged()
                } else if (loadStatus == OrderListItemFragment.LOAD_MORE) {
                    mRootView.smartRefreshlayout_mycollection.finishLoadMore()
                    adapter.notifyDataSetChanged()
                } else {
                    adapter.notifyDataSetChanged()
                }
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }

        }))

    }

    companion object {
        @JvmStatic
        fun newInstance(): MyCollectionFragment {
            return MyCollectionFragment()
        }
    }
}